package com.boilerCloud.controller.msg;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.msg.AppMessages;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.msg.MessageService;
import com.boilerCloud.util.HttpUtil;



/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年3月21日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/message/")
public class AppMessagesControler {
	private static final Logger log = LoggerFactory.getLogger(AppMessagesControler.class);
	/*@Resource
	private IPushService service;*/
	
	@Resource
	private MessageService msgService;
	

	@RequestMapping(value = "goMessagesInfo")
	public ModelAndView goMessagesInfo() {
		return new ModelAndView("/views/message/messagesInfo.html");
	}
	

	/**
	 * 查询反馈消息列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryFeedbackMsgList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryDeviceList(@ModelAttribute AppMessages msg ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<AppMessages> page = new HzlqswPage<AppMessages>();
			page.setCurPage(yema);
			page.setPageSize(length);
			//service.qryFeedbackMsgList(msg,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询反馈消息列表失败");
			log.error("查询反馈消息列表异常",e);
			return rst;
		}
	}
	/**
	 * 查询消息列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryMsgList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryMsgList(@ModelAttribute AppMessages msg ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<AppMessages> page = new HzlqswPage<AppMessages>();
			page.setCurPage(yema);
			page.setPageSize(length);
			msgService.qryMsgList(msg,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
			
			
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询反馈消息列表失败");
			log.error("查询反馈消息列表异常",e);
			return rst;
		}
	}
	
	
	/**
	 * 新增反馈消息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addMsgInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addMsgInfo(
			@ModelAttribute AppMessages msg ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			String ipAddr = HttpUtil.getUsrIPAddr(request);
			msg.setIpAddress(ipAddr);
			msgService.addMsg(msg);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("添加消息失败");
			log.error("添加消息失败",e);
			return rst;
		}
	}
	
	/**
	 * @Title: editMsgInfo
	 * @Description:(一句话阐述):回复消息信息
	 * @param msg
	 * @param request
	 * @return    
	 * @author Ran_cc
	 * @date 2018年3月21日 
	 */
	@RequestMapping(value = "editMsgInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editMsgInfo(
			@ModelAttribute AppMessages msg ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			msgService.editMsg(msg);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("添加消息失败");
			log.error("添加消息失败",e);
			return rst;
		}
	}
	/**
	 * @Title: editMsgInfo
	 * @Description:(一句话阐述):回复消息信息
	 * @param msg
	 * @param request
	 * @return    
	 * @author Ran_cc
	 * @date 2018年3月21日 
	 */
	@RequestMapping(value = "delMsgInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delMsgInfo(
			@ModelAttribute AppMessages msg ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			msg.setIsDelete("y");
			msgService.editMsg(msg);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("添加消息失败");
			log.error("添加消息失败",e);
			return rst;
		}
	}
	
	
	

	

}
